package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private RedissonClient redissonClient;
    @Override
    public Map<String, Object> getItem(Long albumId) {
//        创建一个返回对象
        HashMap<String, Object> map = new HashMap<>();
//        去布隆过滤器中查询，专辑是否存在，如果不存在就返回空
//        RBloomFilter bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        if (!bloomFilter.contains(albumId)){
//            System.out.println("专辑id不存在");
//            return map;
//        }
//        使用CompletableFuture去异步执行操作1
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //        远程调用根据id获取对应的userInfo数据
            Result<AlbumInfo> albumInfoFeignClientAlbumInfdo = albumInfoFeignClient.getAlbumInfo(albumId);
//        判断远程调用获取的对象是否为空
            Assert.notNull(albumInfoFeignClientAlbumInfdo, "远程调用获取albumInfo信息失败");
//        从返回结果中取出对应的AlbumInfo
            AlbumInfo albumInfo = albumInfoFeignClientAlbumInfdo.getData();
//            判断获取的结果是否为空
            Assert.notNull(albumInfo, "远程调用获取albumInfo信息失败");
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor);
        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
//            远程调用获取albumStatVo
            Result<AlbumStatVo> albumStatFeignClientAlbumStat = albumInfoFeignClient.getAlbumStat(albumId);
//            判断远程调用获取的结果是否为空
            Assert.notNull(albumStatFeignClientAlbumStat, "远程调用获取albumStatVo失败");
//            从中获取结果
            AlbumStatVo albumStatData = albumStatFeignClientAlbumStat.getData();
//            判断结果是否为空
            Assert.notNull(albumStatData, "远程调用获取albumStatVo失败");
//            直接放入信息
            map.put("albumStatVo", albumStatData);
        }, threadPoolExecutor);
        CompletableFuture<Void> baseCategoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync((albumInfo) -> {
//            远程调用获取对应的结果
            Result<BaseCategoryView> categoryInfo = categoryFeignClient.getCategoryInfo(albumInfo.getCategory3Id());
//            判断是否为空
            Assert.notNull(categoryInfo, "远程调用获取basecategoryview失败");
//            获取对应的数据
            BaseCategoryView baseCategoryView = categoryInfo.getData();
//            判断是否为空
            Assert.notNull(baseCategoryView, "远程调用获取basecategoryview失败");
//            放入信息
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor);
        CompletableFuture<Void> userInfoCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync((albumInfo -> {
//            远程调用获取对应的结果数据
            Result<UserInfoVo> userInfoVo = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
//            检查是否为空
            Assert.notNull(userInfoVo, "远程调用获取userInfo数据失败");
//          从中获取到对应的data
            UserInfoVo infoVo = userInfoVo.getData();
//            检查是否为空
            Assert.notNull(infoVo, "远程调用获取userInfo数据失败");
//            将数据放入
            map.put("announcer", infoVo);

        }), threadPoolExecutor);
//        调用方法,执行所有异步操作
        CompletableFuture.allOf(albumInfoCompletableFuture,
                albumStatVoCompletableFuture,
                baseCategoryCompletableFuture,
                userInfoCompletableFuture).join();

//        将结果返回
        return map;
    }
}
